<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item
        :label="`${$schoolName ? $schoolName : '学校'}级别`"
        prop="levelName"
      >
        <el-input
          v-model="queryParams.levelName"
          :placeholder="`请输入${$schoolName ? $schoolName : '学校'}级别`"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          v-preventReClick
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button
          v-preventReClick
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-preventReClick
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-preventReClick
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <div class="commonMenu">
      <el-menu
        :default-active="activeIndex"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
      >
        <el-menu-item index="">全部</el-menu-item>
        <el-menu-item index="0">已启用</el-menu-item>
        <el-menu-item index="1">已停用</el-menu-item>
      </el-menu>
    </div>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="级别编号" align="center" prop="levelNum"/>
      <el-table-column label="级别名称" align="center" prop="levelName"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_normal_disable"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="`${$courseName ? $courseName : '课程'}数量`"
        align="center"
        prop="courseNum"
      />
      <el-table-column label="教师数量" align="center" prop="teacherNum"/>
      <el-table-column label="学生数量" align="center" prop="studentNum"/>
      <el-table-column label="云存储" align="center" prop="storage"/>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-preventReClick
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >编辑
          </el-button
          >
          <el-button
            v-preventReClick
            size="mini"
            type="text"
            icon="el-icon-open"
            @click="handleSwitch(scope.row)"
          >{{ scope.row.status == 0 ? '停用' : '启用' }}
          </el-button
          >
          <el-button
            v-preventReClick
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或编辑岗位对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form @submit.native.prevent ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="级别名称" prop="levelName">
          <el-input
            v-model="form.levelName"
            placeholder="请输入级别名称"
            maxlength="20"
            show-word-limit
          />
        </el-form-item>
        <el-form-item
          :label="`${$courseName ? $courseName : '课程'}数量`"
          prop="courseNum"
        >
          <el-input-number
            :min="0"
            v-model="form.courseNum"
            placeholder="请输入"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="教师数量" prop="teacherNum">
          <el-input-number
            :min="0"
            v-model="form.teacherNum"
            placeholder="请输入"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="学生数量" prop="studentNum">
          <el-input-number
            :min="0"
            v-model="form.studentNum"
            placeholder="请输入"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="储存空间" prop="storage">
          <div style="display: flex">
            <el-input-number
              :min="0"
              v-model="form.storage"
              placeholder="请输入"
              style="margin-right: 10px"
            ></el-input-number>
            G
          </div>
        </el-form-item>
        <el-form-item label="级别简介">
          <el-input
            maxlength="500"
            show-word-limit
            :rows="4"
            v-model="form.content"
            type="textarea"
            placeholder="请输入级别简介"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-preventReClick @click="cancel">取 消</el-button>
        <el-button v-preventReClick type="primary" @click="submitForm"
        >确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPost,
  getPost,
  delPost,
  addPost,
  updatePost
} from '@/api/school/level'

export default {
  name: 'Post',
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      activeIndex: '',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        levelName: undefined
      },
      levels: [
        { id: 1, name: '级别一' },
        { id: 2, name: '级别二' }
      ],
      // 表单参数
      form: {
        status: undefined,
        levelName: undefined,
        content: undefined,
        courseNum: undefined,
        studentNum: undefined,
        storage: undefined,
        teacherNum: undefined
      },
      // 表单校验
      rules: {
        levelName: [
          { required: true, message: '级别名称不能为空', trigger: 'blur' }
        ],
        courseNum: [
          {
            required: true,
            message: `${
              this.$courseName ? this.$courseName : '课程'
            }数量不能为空`,
            trigger: 'blur'
          }
        ],
        teacherNum: [
          { required: true, message: '教师数量不能为空', trigger: 'blur' }
        ],
        studentNum: [
          { required: true, message: '学生数量不能为空', trigger: 'blur' }
        ],
        storage: [{ required: true, message: '内存不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // el-menu
    handleSelect(key) {
      this.queryParams.status = key
      this.getList()
    },
    // 停用 启用按钮
    handleSwitch(row) {
      this.$confirm(`是否确认${row.status == 0 ? '停用' : '启用'}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let data = Object.assign({}, row)
          data.status = row.status == 0 ? 1 : 0
          updatePost(data).then(() => {
            this.$modal.msgSuccess('编辑成功')
            this.open = false
            this.getList()
          })
        })
        .catch(() => {
        })
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      listPost(this.queryParams).then((response) => {
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        levelName: undefined,
        content: undefined,
        courseNum: undefined,
        studentNum: undefined,
        storage: undefined,
        teacherNum: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.levelId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加级别'
    },
    /** 编辑按钮操作 */
    handleUpdate(row) {
      this.reset()
      const levelId = row.levelId || this.ids
      getPost(levelId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '编辑级别'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.levelId != undefined) {
            updatePost(this.form).then(() => {
              this.$modal.msgSuccess('编辑成功')
              this.open = false
              this.getList()
            })
          } else {
            addPost(this.form).then(() => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.levelId || this.ids
      this.$modal
        .confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？')
        .then(function() {
          return delPost(postIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/post/export',
        {
          ...this.queryParams
        },
        `post_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
<style scoped lang="scss">
::v-deep .el-menu-item:hover {
  border-bottom: 1px solid #e6e6e6 !important;
}

::v-deep .el-menu--horizontal > .el-menu-item.is-active:hover {
  border-bottom: 2px solid #11a983 !important;
}
</style>
